<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Input extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url_helper');
		$this->load->database();
		show_404('', FALSE);
    }

    public function index(){
    	set_time_limit (0);
    	
    	$regular[1] = '/\<td\sheight[\s\S]+?\>([\s\S]+?)\<\/td\>\s+\<td\salign\=\"left\"[\s\S]+?\>([\s\S]+?)\<[\s\S]+?\>\s+\<td[\s\S]+?\>([\s\S]+?)\</u';
    	$regular[2] = '/\<td\sheight=[\s\S]+?\>([\s\S]+?)\<\/[\s\S]+?\<td[[\s\S]+?\>([\s\S]+?)\<\/td\>[\s\S]+?\>(\w+)/u';
    	$regular[3] = '/\<td\sheight=[\s\S]+?\>([\s\S]+?)\<\/[\s\S]+?\<td[[\s\S]+?\>(\w)/u';

    	for ($i = 1; $i < 4; $i++) { 
	    	$question = file_get_contents('./data/'.$i.'.txt');
	    	preg_match_all($regular[$i],$question,$mat);
	    	// print_r($mat);
	    	// $sql = "INSERT INTO question (title, content, answer, type) VALUES ('".$mat[1][0]."', '".$mat[2][0]."','".$mat[3][0]."','单选')";
	    	for ($j = 0; $j < count($mat[1]); $j++) {
	    		if($i == 1){
	    			$type = '单选';
	    		}
	    		elseif ($i == 2) {
	    			$type = '多选';
	    		}
	    		elseif ($i == 3){
	    			$type = '判断';
	    			$mat[3][$j] = $mat[2][$j];
	    			$mat[2][$j] = '';
	    		}
	    		$data = array('title' => $mat[1][$j], 'content' => $mat[2][$j], 'answer' => $mat[3][$j], 'type' => $type);
	    		$sql = $this->db->insert_string('question', $data);
	    		// 用函数会导致效率降低。
				// $sql = "INSERT INTO question (title, content, answer, type) VALUES ('".$mat[1][$j]."', '".$mat[2][$j]."','".$mat[3][$j]."','".$type."')";
				var_dump($this->db->query($sql));
	    	}
    	}
    }
}